package com.es.hr.baoxiaoShenpi;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.es.business.area.AreaBean;
import com.es.business.area.AreaBiz;
import com.es.business.city.CityBean;
import com.es.business.city.CityBiz;
import com.es.business.citys.CitysBean;
import com.es.business.citys.CitysBiz;
import com.es.business.province.ProvinceBean;
import com.es.business.province.ProvinceBiz;
import com.es.common.CommonBiz;
import com.es.hr.baoxiao.BaoxiaoBean;
import com.es.hr.baoxiao.BaoxiaoBiz;
import com.es.hr.emp.EmpInfoBean;
import com.es.main.LoginUserBeen;
import com.es.util.mail.MailInfo;
import com.es.util.mail.SendMail;
import com.es.util.page.PageBean;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.ActionSupport;

public class BaoxiaoShenpiPiliangPassAction extends ActionSupport
implements ServletRequestAware{
	 HttpServletRequest request;

	public void setServletRequest(HttpServletRequest paramHttpServletRequest) {
		// TODO Auto-generated method stub
		this.request = paramHttpServletRequest;
	}
	
	public String execute() throws Exception{
		String result = "";
		String message = "";
		BaoxiaoShenpiBiz baoxiaoShenpiBiz = new BaoxiaoShenpiBiz();
		
		String isZongbu = this.request.getParameter("isZongbu");
		this.request.setAttribute("isZongbu", isZongbu);
		String area_code = this.request.getParameter("emp_area_code");
		this.request.setAttribute("emp_area_code", area_code);
		
		String province_code = this.request.getParameter("emp_province_code");
		this.request.setAttribute("emp_province_code", province_code);
		String city_code = this.request.getParameter("emp_city_code");
		this.request.setAttribute("emp_city_code", city_code);
		
		String rtm_code = this.request.getParameter("rtm_code");
		String rtm_name = this.request.getParameter("rtm_name");
		String ctm_code = this.request.getParameter("ctm_code");
		String ctm_name = this.request.getParameter("ctm_name");
		this.request.setAttribute("rtm_code", rtm_code);
		this.request.setAttribute("rtm_name", rtm_name);
		this.request.setAttribute("ctm_code", ctm_code);
		this.request.setAttribute("ctm_name", ctm_name);
		
		this.request.setAttribute("areaList", new AreaBiz().getAreaList());

		if(area_code.length() > 0){
			this.request.setAttribute("provinceList", new ProvinceBiz().getProvinceByAreaCode(area_code));
		}
		if(province_code.length() > 0){
			this.request.setAttribute("cityList", new CityBiz().getCityByProvinceCode(province_code));
		}

		
		Date date = new Date();
		int year = date.getYear() + 1900;
		int mouth = date.getMonth() + 1;
		int day = date.getDate();
		
		//限制5号以内才能报销审批
		com.es.common.CommonBean riqibean = new CommonBiz().getCommonByTypeAndCode("baoxiaoproperty", "baoxiaoshenpiriqi");
		int riqi = riqibean == null?3:new Integer(riqibean.getCommonValue()).intValue();
		if(day > riqi){
			message = "每个月前"+riqi+"天才能审批";
			this.request.setAttribute("message", message);
			return "error";
		}
		
		String syear = new Integer(year).toString();
		String smouth = new Integer(mouth).toString();
		if(smouth.length() < 2) smouth = "0" + smouth;
		String sday = new Integer(day).toString();
		if(sday.length() < 2) sday = "0" + sday;
		String pizhun_date = syear + "-" + smouth + "-" + sday; 
		 String[] baoxiao_shenpi_ids = (String[]) this.request.getParameterMap().get("baoxiao_shenpi_id");
		 String[] baoxiao_shenpi_tds = (String[]) this.request.getParameterMap().get("check");
		if(isZongbu.equals("true")){
			for(int i = 0; i < baoxiao_shenpi_ids.length; ++i){
				if(baoxiao_shenpi_tds[i].equals("true")){
					baoxiaoShenpiBiz.zongbuPizhun(baoxiao_shenpi_ids[i], pizhun_date, "1", "同意");
					BaoxiaoShenpiBean bean = baoxiaoShenpiBiz.searchBaoxiaoShenpiById(baoxiao_shenpi_ids[i]);
					sendMailToCTM(bean, true);
				}	
			}
			
		}else {
			for(int i = 0; i < baoxiao_shenpi_ids.length; ++i){
				if(baoxiao_shenpi_tds[i].equals("true")){
					baoxiaoShenpiBiz.rtmPizhun(baoxiao_shenpi_ids[i], pizhun_date, "1", "同意");
					BaoxiaoShenpiBean bean = baoxiaoShenpiBiz.searchBaoxiaoShenpiById(baoxiao_shenpi_ids[i]);
					sendMailToCTM(bean, false);
				}
					
			}
		}
		message = "批量批准成功，并发邮件通知相应的CTM";
		this.request.setAttribute("message", message);
		result = "success";
		return result;
		
	}
	
	public void sendMailToCTM(BaoxiaoShenpiBean baoxiaoShenpiBean, boolean isZongbu) throws IOException{
		String lingdao ="";
		if(isZongbu == false){
			lingdao = "RTM: " + baoxiaoShenpiBean.rtm_name;
		}else{
			lingdao = " 事业部  ";
		}
		MailInfo mailinfo=new MailInfo();  
        String [] toAddress= new String[1];
        toAddress[0] = baoxiaoShenpiBean.ctm_mail;
        mailinfo.setToAddress(toAddress); 
        
        mailinfo.setSubject("re: 关于报销"); 
        String contentStart = "<a href='#'>";
        String contentEnd = "</a>";
        String content = "";
        content += " 报销通过：";
        content += baoxiaoShenpiBean.ctm_name + " 您好，\n";
        content +=" 您 "+baoxiaoShenpiBean.baoxiao_lastmodify_time.substring(0,7) +" 报销申请 " + lingdao + " 已批复！\n 请您查看";
        mailinfo.setContent(contentStart + content + contentEnd);//网页内容   
        SendMail sm=new SendMail();   
        sm.sendHtmlMail(mailinfo);
}

}
